package com.example.lotterysystem.common.interceptor;

import com.example.lotterysystem.common.utils.JWTUtil;
import com.example.lotterysystem.controller.UserController;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component

public class LoginInterceptor implements HandlerInterceptor{

@Autowired
    private final static Logger logger= LoggerFactory.getLogger(LoginInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        logger.info("获取token：{}",token);
        logger.info("拦截器拦截请求：{}",request.getRequestURI());
        Claims claims= JWTUtil.parseJWT(token);
        if(null==claims){
            logger.error("解析JWT令牌失败");
            response.setStatus(401);
            return false;
        }
        logger.info("解析JWT令牌成功");
        return true;
    }
}

